package com.szxb.facebus.util.basics;

import com.szxb.mlog.SLog;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Tangren on 2018-12-10
 * 包名：com.szxb.facebus.util.basics
 * 邮箱：996489865@qq.com
 * TODO:一句话描述
 */

public class HexUtil {

    public static byte[] readFile(File file) {
        try {
            FileInputStream stream = new FileInputStream(file);
            int length = stream.available();
            byte[] buffer = new byte[length];
            int offset = 0;
            int numRead = 0;
            while (offset < buffer.length
                    && (numRead = stream.read(buffer, offset, buffer.length - offset)) >= 0) {
                offset += numRead;
            }
            stream.close();
            return buffer;
        } catch (IOException e) {
            e.printStackTrace();
            SLog.e("FileUtil(readFile.java:66)读取文件失败>>" + Utils.getExectionStr(e));
        }
        return null;
    }


    public static byte[] File2byte(String filePath) {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (Exception e) {
            SLog.e("File2byte(FileByte.java:39)文件读取失败>>" + e.getMessage());
            e.printStackTrace();

        }
        return buffer;
    }


    /**
     * 读取文件
     *
     * @param path .
     * @return .
     */
    public static List<String> readFileToListString(File path) {
        List<String> list = new ArrayList<>();
        try {
            BufferedReader br = new BufferedReader(new FileReader(path));
            String readline = "";
            while ((readline = br.readLine()) != null) {
                list.add(readline);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * @param path 路径
     * @return 返回String
     */
    public static String readFileToString(File path) {
        try {
            StringBuilder fileData = new StringBuilder();
            BufferedReader reader = new BufferedReader(new FileReader(path));
            char[] buff = new char[1024];
            int numRead = 0;
            while ((numRead = reader.read(buff)) != -1) {
                String readData = String.valueOf(buff, 0, numRead);
                fileData.append(readData);
            }
            reader.close();
            return fileData.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
